package com.pubinfo.passbook.api.controller.auth;

import com.pubinfo.passbook.common.entity.TEnginePlaceholderEquation;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.dto.engine.EngineEquationParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.aviator.AviatorScriptService;
import com.pubinfo.passbook.common.service.engine.EngineComputePlaceHolderEquationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author zhouch
 * @date 2021/12/20 16:48
 */
@Api(tags = "计算引擎-公式占位符管理")
@RestController
@RequestMapping("/engine/placeholder/equation")
public class EngineEquationController {

    @Resource
    EngineComputePlaceHolderEquationService equationService;

    @Resource
    AviatorScriptService scriptService;
  
  @PostMapping("/page")
  @ApiOperation("分页查询")
  public JsonResult<MyPage<TEnginePlaceholderEquation>> page(@RequestBody @Valid EngineEquationParam param) {
    return JsonResult.page(equationService.page(param));
  }


    @PostMapping("/save")
    @ApiOperation("保存")
    public JsonResult<Boolean> save(@RequestBody @Valid TEnginePlaceholderEquation equationInfo) {
        if (equationInfo.getId() != null) {
            throw new ServiceException("ID字段存在");
        }

        return JsonResult.success(equationService.save(equationInfo));
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    public JsonResult<Boolean> update(@RequestBody @Valid TEnginePlaceholderEquation equationInfo) {
        if (equationInfo.getId() == null) {
            throw new ServiceException("ID字段不存在");
        }

        return JsonResult.success(equationService.updateById(equationInfo));
    }

    @GetMapping("/remove/{id}")
    @ApiOperation("删除")
    public JsonResult<Boolean> remove(@PathVariable("id") Integer id) {
        return JsonResult.success(equationService.removeById(id));
    }

    @GetMapping("/get/{id}")
    @ApiOperation("详情")
    public JsonResult<TEnginePlaceholderEquation> get(@PathVariable("id") Integer id) {
        return JsonResult.success(equationService.getById(id));
    }

    @PostMapping("/validate")
    @ApiOperation("检查公式")
    public JsonResult<Boolean> validate(@RequestParam("equation") String equation) {
        return JsonResult.success(scriptService.validate(equation));
    }
}
